package com.yhyvr.api.service.store;

import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoProxyShippingAddressMapper;
import com.lz.mybatis.jdbc.auto.model.BoProxyShippingAddress;
import com.lz.mybatis.jdbc.auto.model.BoProxyShippingAddressExample;
import com.lz.tool.ListUtils;
import com.lz.tool.UUIDTool;
import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.address.ApiAddressDefaultSetReq;
import com.yhyvr.pojo.req.api.store.address.ApiAddressSaveUpdateReq;
import com.yhyvr.pojo.resp.api.ApiRespBody;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.ApiResultCode;
import com.yhyvr.pojo.resp.api.store.address.ApiAddressInfoResp;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by tech6 on 2016/10/14.
 * 配送地址服务
 */

@Service
public class ApiProxyShippingAddressService {


    @Resource
    private BoProxyShippingAddressMapper boProxyShippingAddressMapper;


    /**
     * 地址列表
     * @param apiReqBodyApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<List<ApiAddressInfoResp>> listAddress(ApiReqData<ApiReqBody> apiReqBodyApiReqData){
            List<ApiAddressInfoResp> listAddress = Lists.newArrayList();
                BoProxyShippingAddressExample boProxyShippingAddressExample = new BoProxyShippingAddressExample();
                boProxyShippingAddressExample.createCriteria().andPrpxyIdEqualTo(apiReqBodyApiReqData.getCookieUserId());
                boProxyShippingAddressExample.setOrderByClause(" default_address desc,create_time desc " );
               List<BoProxyShippingAddress> boProxyShippingAddressList =  boProxyShippingAddressMapper.selectByExample(boProxyShippingAddressExample);
                if(boProxyShippingAddressList!=null){
                    for (BoProxyShippingAddress boProxyShippingAddress : boProxyShippingAddressList) {
                        ApiAddressInfoResp apiAddressInfoResp = new ApiAddressInfoResp();
                        apiAddressInfoResp.setAddress(boProxyShippingAddress.getShippingDetailAddress());
                        apiAddressInfoResp.setAddressId(boProxyShippingAddress.getShippingAddressId());
                        apiAddressInfoResp.setCity(boProxyShippingAddress.getShippingCity());
                        apiAddressInfoResp.setCounty(boProxyShippingAddress.getShippingCounty());
                        apiAddressInfoResp.setConsignee(boProxyShippingAddress.getConsigneeName());
                        apiAddressInfoResp.setPhone(boProxyShippingAddress.getConsigneePhone());
                        apiAddressInfoResp.setProvince(boProxyShippingAddress.getShippingProvince());
                        apiAddressInfoResp.setDefaultAddress(boProxyShippingAddress.getDefaultAddress());
                        apiAddressInfoResp.setPostCode(boProxyShippingAddress.getShippingPostcode());
                        listAddress.add(apiAddressInfoResp);
                    }
                    return ApiRespData.buildSucc(listAddress);
                }
        return ApiRespData.buildFail();
    }



    /**
     * 添加地址
     * @param apiReqBodyApiReqData
     * @return
     */
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Transactional
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> addAddress(ApiReqData<ApiAddressSaveUpdateReq> apiReqBodyApiReqData){
        ApiAddressSaveUpdateReq address = apiReqBodyApiReqData.getBody();
        String proxyId =apiReqBodyApiReqData.getCookieUserId();
                BoProxyShippingAddress boProxyShippingAddress = new BoProxyShippingAddress();
                if(address.getDefaultAddress()==1){
                    String sql="update bo_proxy_shipping_address set default_address = 0 where prpxy_id='"+proxyId+"'";
                    jdbcTemplate.execute(sql);
                }
                boProxyShippingAddress.setDefaultAddress(address.getDefaultAddress());
                boProxyShippingAddress.setPrpxyId(proxyId);
                boProxyShippingAddress.setConsigneeName(address.getConsignee());
                boProxyShippingAddress.setConsigneePhone(address.getPhone());
                boProxyShippingAddress.setShippingCity(address.getCity());
                boProxyShippingAddress.setShippingCounty(address.getCounty());
                boProxyShippingAddress.setShippingDetailAddress(address.getAddress());
                boProxyShippingAddress.setShippingPostcode(address.getPostCode());
                boProxyShippingAddress.setShippingProvince(address.getProvince());
                boProxyShippingAddress.setShippingAddressId(UUIDTool.getUUID());
                boProxyShippingAddressMapper.insertSelective(boProxyShippingAddress);
                    return ApiRespData.buildSucc();
    }

    /**
     * 地址详情
     * @param apiReqBodyApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiAddressInfoResp> addressInfo(ApiReqData<ApiAddressDefaultSetReq> apiReqBodyApiReqData){
                BoProxyShippingAddress boProxyShippingAddress =boProxyShippingAddressMapper.selectByPrimaryKey(apiReqBodyApiReqData.getBody().getAddressId());
                if(boProxyShippingAddress!=null){
                    ApiAddressInfoResp apiAddressInfoResp = new ApiAddressInfoResp();
                    apiAddressInfoResp.setAddress(boProxyShippingAddress.getShippingDetailAddress());
                    apiAddressInfoResp.setAddressId(boProxyShippingAddress.getShippingAddressId());
                    apiAddressInfoResp.setCity(boProxyShippingAddress.getShippingCity());
                    apiAddressInfoResp.setCounty(boProxyShippingAddress.getShippingCounty());
                    apiAddressInfoResp.setConsignee(boProxyShippingAddress.getConsigneeName());
                    apiAddressInfoResp.setPhone(boProxyShippingAddress.getConsigneePhone());
                    apiAddressInfoResp.setProvince(boProxyShippingAddress.getShippingProvince());
                    apiAddressInfoResp.setDefaultAddress(boProxyShippingAddress.getDefaultAddress());
                    apiAddressInfoResp.setPostCode(boProxyShippingAddress.getShippingPostcode());
                    return ApiRespData.buildSucc(apiAddressInfoResp);
                }else{
                    return ApiRespData.buildFailErrorCode(ApiResultCode.FAIL,"10002","没有找到地址");
                }

    }


    /**
     * 默认地址
     * @param apiReqBodyApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiAddressInfoResp> defaultAddress(ApiReqData<ApiReqBody> apiReqBodyApiReqData){
                String proxyId = apiReqBodyApiReqData.getCookieUserId();
                BoProxyShippingAddressExample boProxyShippingAddressExample = new BoProxyShippingAddressExample();
                boProxyShippingAddressExample.createCriteria().andPrpxyIdEqualTo(proxyId).andDefaultAddressEqualTo(1);
                BoProxyShippingAddress boProxyShippingAddress = ListUtils.getFirst(boProxyShippingAddressMapper.selectByExample(boProxyShippingAddressExample));
               if(boProxyShippingAddress!=null){
                   ApiAddressInfoResp apiAddressInfoResp = new ApiAddressInfoResp();
                   apiAddressInfoResp.setAddress(boProxyShippingAddress.getShippingDetailAddress());
                   apiAddressInfoResp.setAddressId(boProxyShippingAddress.getShippingAddressId());
                   apiAddressInfoResp.setCity(boProxyShippingAddress.getShippingCity());
                   apiAddressInfoResp.setCounty(boProxyShippingAddress.getShippingCounty());
                   apiAddressInfoResp.setConsignee(boProxyShippingAddress.getConsigneeName());
                   apiAddressInfoResp.setPhone(boProxyShippingAddress.getConsigneePhone());
                   apiAddressInfoResp.setProvince(boProxyShippingAddress.getShippingProvince());
                   apiAddressInfoResp.setDefaultAddress(boProxyShippingAddress.getDefaultAddress());
                   apiAddressInfoResp.setPostCode(boProxyShippingAddress.getShippingPostcode());
                   return ApiRespData.buildSucc(apiAddressInfoResp);
               }else{
                   return ApiRespData.buildFailErrorCode(ApiResultCode.FAIL,"10002","没有设置默认地址");
               }

    }

    /**
     * 更新地址
     * @param apiReqBodyApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    @Transactional
    public ApiRespData<ApiRespBody> updateAddress(ApiReqData<ApiAddressSaveUpdateReq> apiReqBodyApiReqData){
        ApiAddressSaveUpdateReq address = apiReqBodyApiReqData.getBody();
                BoProxyShippingAddress boProxyShippingAddress = boProxyShippingAddressMapper.selectByPrimaryKey(address.getAddressId());
                String proxyId = apiReqBodyApiReqData.getCookieUserId();
                if(address.getDefaultAddress()==1){
                    String sql="update bo_proxy_shipping_address set default_address = 0 where prpxy_id='"+proxyId+"'";
                    jdbcTemplate.execute(sql);
                }
                boProxyShippingAddress.setDefaultAddress(address.getDefaultAddress());
                boProxyShippingAddress.setPrpxyId(proxyId);
                boProxyShippingAddress.setConsigneeName(address.getConsignee());
                boProxyShippingAddress.setConsigneePhone(address.getPhone());
                boProxyShippingAddress.setShippingCity(address.getCity());
                boProxyShippingAddress.setShippingCounty(address.getCounty());
                boProxyShippingAddress.setShippingDetailAddress(address.getAddress());
                boProxyShippingAddress.setShippingPostcode(address.getPostCode());
                boProxyShippingAddress.setShippingProvince(address.getProvince());
                boProxyShippingAddressMapper.updateByPrimaryKeySelective(boProxyShippingAddress);
                return ApiRespData.buildSucc();
    }

    /**
     * 设置默认收货地址
     * @param apiReqBodyApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    @Transactional
    public ApiRespData<ApiRespBody> setDefaultAddress(ApiReqData<ApiAddressDefaultSetReq> apiReqBodyApiReqData){
        ApiAddressDefaultSetReq address = apiReqBodyApiReqData.getBody();
                String proxyId = apiReqBodyApiReqData.getCookieUserId();
                String sql="update bo_proxy_shipping_address set default_address = 0 where prpxy_id='"+proxyId+"'";
                jdbcTemplate.execute(sql);
                BoProxyShippingAddress boProxyShippingAddress = boProxyShippingAddressMapper.selectByPrimaryKey(address.getAddressId());
                boProxyShippingAddress.setPrpxyId(proxyId);
                boProxyShippingAddress.setDefaultAddress(1);
                boProxyShippingAddressMapper.updateByPrimaryKeySelective(boProxyShippingAddress);
                return ApiRespData.buildSucc();
    }


    /**
     * 删除收货地址
     * @param apiReqBodyApiReqData
     * @return
     */
    @Transactional
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> deleteAddress(ApiReqData<ApiAddressDefaultSetReq> apiReqBodyApiReqData){
        ApiAddressDefaultSetReq address = apiReqBodyApiReqData.getBody();
                String proxyId = apiReqBodyApiReqData.getCookieUserId();
                BoProxyShippingAddressExample boProxyShippingAddressEx = new BoProxyShippingAddressExample();
                boProxyShippingAddressEx.createCriteria().andPrpxyIdEqualTo(proxyId).andShippingAddressIdEqualTo(address.getAddressId());
                boProxyShippingAddressMapper.deleteByExample(boProxyShippingAddressEx);
                return ApiRespData.buildSucc();
    }
}
